Skip to content

feat(nix): add flake with package, overlay and devShells - #104

Open
WangYiBen0 wants to merge 2 commits into
std-microblock:masterfrom
WangYiBen0:nix-flake
Open

WangYiBen0 wants to merge 2 commits into
std-microblock:masterfrom
WangYiBen0:nix-flake

Conversation

@WangYiBen0

Copy link
Copy Markdown

Summary

Adds a Nix flake: packages.celemod, packages.celemod-ui, overlays.default
and devShells.{default,ci} for x86_64-linux and aarch64-linux.

nix build .#celemod
nix run   .#celemod
nix develop
nix flake check

Verified

check result
nix eval .#packages.x86_64-linux.celemod.version 1.2.2, read from version.txt
nix build .#packages.x86_64-linux.celemod exit 0
$out/bin/ cele-mod (GTK wrapper) + .cele-mod-wrapped
nix flake check --all-systems --no-build exit 0

Implementation notes

Constraints of this nixpkgs or this repository, not preferences. Each is
commented at the relevant line and written up in nix/README.md.

  • Nightly Rust is required. src-tauri/src/lib.rs uses #![feature(try_blocks)], so a stable rustc fails with E0554. rustPlatform comes from rust-overlay's rust-bin.nightly.latest.default; the component .rustc ships no std sysroot (E0463).
  • cargo-tauri.hook drives the build. It runs tauri-build with the custom-protocol feature, so the frontend is embedded instead of loaded from build.devUrl at runtime. Building without it produces a binary that opens a window and shows nothing.
  • wrapGAppsHook4 supplies the GTK runtime environment (XDG_DATA_DIRS, GDK_PIXBUF_MODULE_PATH, GIO modules) and installs the desktop entry and icons.
  • cargoHash is top-level and cargoLock absent. buildRustPackage prefers cargoLock whenever it is set and silently ignores a top-level cargoHash, then fails with No hash was found while vendoring the git dependency game-scanner-1.2.0.
  • cargoRoot = ".". It also drives vendor staging, so any other value makes it look for Cargo.lock outside the workspace root; buildAndTestSubdir selects src-tauri.
  • CMAKE_POLICY_VERSION_MINIMUM = 3.5. prost-build 0.10.4 vendors protobuf and runs CMake on a pre-3.5 CMakeLists.txt.
  • rust-overlay is pinned by rev, not HEAD. A floating ref makes every evaluation call the GitHub API, which rate-limits.
  • nix/ must stay tracked. A git-repo flake cannot see untracked files.

Hashes

All three hashes in nix/versions.nix came from real fetch failures, never
guesses. scripts/update-nix-hashes.sh recomputes them, validates every
digest, asserts the written value matches the one just measured, and is
byte-identical on a second run.

Guard

checks.<system>.celemod-is-real fails the build if either package stops being
a real derivation, because nix flake check --no-build cannot tell a real
derivation from an empty runCommand stub. It asserts on nativeBuildInputs
rather than passthru.placeholder, which a plain runCommand never sets.

Scope

Desktop builds only. Android, AppImage/deb bundling and tauri build output
artifacts are untouched.


Adds a flake exposing packages.celemod, packages.celemod-ui,
overlays.default and devShells.{default,ci} for x86_64-linux and
aarch64-linux.

The package builds and runs:

  nix build .#celemod
  nix run   .#celemod

The Vite frontend is a separate derivation driven by fetchPnpmDeps. The Tauri
shell builds through cargo-tauri.hook, which runs tauri-build with the
custom-protocol feature so the finished dist/ is embedded; without it the
binary loads build.devUrl at runtime and shows an empty window.
wrapGAppsHook4 supplies the GTK runtime environment (XDG_DATA_DIRS,
GDK_PIXBUF_MODULE_PATH, GIO modules) and installs the desktop entry and icons.

Constraints worth knowing, each commented at the relevant line and written up
in nix/README.md:

  * nightly Rust is required (src-tauri/src/lib.rs uses #![feature(try_blocks)]);
    rustPlatform comes from rust-overlay's nightly .default toolchain
  * cargoHash must be top-level and cargoLock absent; supplying both makes
    buildRustPackage prefer importCargoLock, which then demands outputHashes
    for the git dependency
  * cargoRoot stays "." so fetchCargoVendor finds the workspace lockfile
  * rust-overlay is pinned by rev, not HEAD, so evaluation never calls the
    GitHub API
  * nix/ must stay git-tracked: a git-repo flake cannot see untracked files

Every hash in nix/versions.nix came from a real fetch failure.
scripts/update-nix-hashes.sh refreshes them idempotently and asserts that what
it writes matches what it measured.

checks.<system>.celemod-is-real guards against the packages collapsing to
runCommand placeholders, which `nix flake check --no-build` cannot detect.

@std-microblock std-microblock left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the write-up is good and the traps you documented are real. A few concrete problems before merge:

Blocking

  1. nix/versions.nix says it can be reduced to { } and the flake still evaluates. It can't: nix/tools.nix unconditionally reads versions.version and versions.pnpm.depsHash, and mkCelemod's default cargoHash ? versions.cargoHash is evaluated too. Fix the comment or the code, otherwise following that instruction breaks the flake.
  2. scripts/update-nix-hashes.sh: --repo-root can never be reached, because the flake.nix existence check runs before the argument parsing. Move the while [ $# -gt 0 ] loop above it.
  3. nix/hooks/celemod.sh and nix/hooks/ci.sh are never sourced by either shellHook. Wire them in or drop them.
  4. The result symlink left by nix build is not in .gitignore. Please add /result (and /result-*).
  5. Nothing in CI runs the flake, while the three hashes have to be recomputed every time Cargo.lock or src/celemod-ui/pnpm-lock.yaml changes. Please add a check job (ubuntu-latest is enough), or say in nix/README.md that this is best effort and who refreshes the hashes.

Nits

  • cargoRoot is described three ways: the PR body says it is required, nix/README.md has a section titled "No cargoRoot", and nix/tools.nix defaults to ".". Please align.
  • nix/versions.nix hardcodes version = "1.2.2" while version.txt is the single source of truth. Drop it or explain why it is there.
  • nix/README.md says the refresher writes a fake hash into the file first; it actually builds a throwaway expression with pkgs.lib.fakeHash. Worth rewording.
  • nix/versions.nix has no trailing newline.

One question: is a Nix package meant to be supported long term here, and who owns the hash refresh when the lockfiles move? That answer decides how much of the above is blocking.

Blocking items:

  * nix/versions.nix claimed it could be reduced to `{ }`. It cannot:
    tools.nix reads cargoHash, pnpm.fetcherVersion and pnpm.depsHash
    unconditionally. The comment now says so.
  * Drop `version` from versions.nix entirely. It duplicated version.txt and
    tools.nix now reads version.txt through builtins.readFile, so the file
    holds hashes only.
  * scripts/update-nix-hashes.sh parsed arguments after validating REPO_ROOT,
    so --repo-root could never be reached. Parse first, then validate.
  * Sourcing nix/hooks/{celemod,ci}.sh from the shellHook instead of shipping
    them unused. ci.sh also drives colourless output, so CARGO_TERM_COLOR in
    mkShell now depends on the mode.
  * Ignore the /result symlinks nix build leaves behind.
  * Add .github/workflows/Nix.yml: evaluates the flake on every relevant
    change, builds the package, and fails when nix/versions.nix is stale
    relative to either lockfile.

Nits:

  * cargoRoot is documented as `cargoRoot = "."` everywhere; the README no
    longer contradicts tools.nix.
  * README describes the refresher accurately: it builds a throwaway
    expression with pkgs.lib.fakeHash rather than writing a fake into the file.
  * versions.nix ends with a newline.

Also renames the package attributes to match the upstream crate, whose
[package] and [[bin]] are both `cele-mod`.
@WangYiBen0

WangYiBen0 commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

nix/versions.nix says it can be reduced to { } and the flake still evaluates. It can't: nix/tools.nix unconditionally reads versions.version and versions.pnpm.depsHash, and mkCelemod's default cargoHash ? versions.cargoHash is evaluated too. Fix the comment or the code, otherwise following that instruction breaks the flake.

你说得对,三处都确实无条件求值。改了代码也改了注释:versions.nix 现在写明这三个属性必需,version 直接删掉,tools.nix 改从 version.txt 读:

repoVersion = lib.strings.trim (builtins.readFile ./../version.txt);

cargoHash 在 lib.nix 里改成显式传入,不再依赖默认值:

inherit (versions) cargoHash;

scripts/update-nix-hashes.sh: --repo-root can never be reached, because the flake.nix existence check runs before the argument parsing. Move the while [ $# -gt 0 ] loop above it.

已调换,先解析后校验。

nix/hooks/celemod.sh and nix/hooks/ci.sh are never sourced by either shellHook. Wire them in or drop them.

已接进 shellHook:default → celemod.sh,ci → ci.sh。都幂等且返回成功,不影响 nix develop --command。

接的过程中发现另一个问题:mkShell 里 CARGO_TERM_COLOR = "always" 会覆盖 ci.sh,所以 ci shell 之前根本没有无色输出(实测 ctc=always)。现在按 mode 取值,实测 ctc=never。

The result symlink left by nix build is not in .gitignore. Please add /result (and /result-*).

已加。

Nothing in CI runs the flake, while the three hashes have to be recomputed every time Cargo.lock or src/celemod-ui/pnpm-lock.yaml changes. Please add a check job (ubuntu-latest is enough), or say in nix/README.md that this is best effort and who refreshes the hashes.

选了加 job,.github/workflows/Nix.yml,触发文件包括那两个 lockfile。三步:

  • nix flake check --all-systems --no-build
  • nix build .#packages.x86_64-linux.cele-mod
  • ./scripts/update-nix-hashes.sh 后接 git diff --exit-code,哈希过期直接失败

cargoRoot is described three ways: the PR body says it is required, nix/README.md has a section titled "No cargoRoot", and nix/tools.nix defaults to ".". Please align.

统一成 cargoRoot = ".",README 那节改了标题,不再是 "No cargoRoot"。

nix/versions.nix hardcodes version = "1.2.2" while version.txt is the single source of truth. Drop it or explain why it is there.

删了。

nix/README.md says the refresher writes a fake hash into the file first; it actually builds a throwaway expression with pkgs.lib.fakeHash. Worth rewording.

已按实际做法重写。

nix/versions.nix has no trailing newline.

已补。

One question: is a Nix package meant to be supported long term here, and who owns the hash refresh when the lockfiles move? That answer decides how much of the above is blocking.

我的意思是长期维护:lockfile 变动时由我刷哈希。脚本是自助式的,CI 也会在 versions.nix 过期时直接失败,不会静默腐烂。如果你不希望仓库里多一个需要跟进的包,关掉 PR 我也没有意见。

另外把包名统一了:上游 [package] 和 [[bin]] 都是 cele-mod,之前我写成 celemod 是错的。现在是 packages.<system>["cele-mod"] → $out/bin/cele-mod,前端产物是 ["cele-mod-ui"] → $out/dist。

改动在 406c930。


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants